package demo.thread.synMethod;

/**
 * 共享数据
 */
public class ShareData {

    private boolean flag = false;

    private static ShareData shareData;

    public static synchronized ShareData getInstance(){
        if(shareData == null){
            shareData = new ShareData();
        }
        return shareData;
    }

    // 对于同步方法来说，锁是当前对象的实例

    public synchronized void setFlag(boolean flag) {
        this.flag = flag;
        notifyAll(); // 通知所有等待的线程
    }

    public synchronized void waitForFlag() throws InterruptedException {
        while (!flag) {
            wait(); // 等待flag变为true
        }
    }
}
